import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class StringLet {

        public static List<Integer> findSubstring(String s, String[] words) {
            int words_num = words.length;
            List<String> wordsList = Arrays.asList(words);
            int s_num = words[0].length();
            List<Integer> result = new ArrayList();
            int sub_string_length = words_num * s_num;
            HashMap<String,Integer> mapStrNums = new HashMap();

            for(String str : words) {
                if(mapStrNums.containsKey(str)){
                    Integer existNum = mapStrNums.get(str);
                    existNum++;
                    mapStrNums.put(str,existNum );
                }else{
                    mapStrNums.put(str,1);
                }
            }



            for(int i = 0;i<=s.length()-sub_string_length;i++){
                int j = 0;
                HashMap<String,Integer> nowMap = new HashMap(mapStrNums);
                while(j<words_num){
                    String sub_sub_string = s.substring(i+(j*s_num),i+(j+1)*s_num);
                    if(!wordsList.contains(sub_sub_string)){
                        break;
                    }
                    Integer existNum = nowMap.get(sub_sub_string);
                    existNum--;
                    int num_new = existNum;
                    if (num_new < 0){
                        break;
                    }
                    nowMap.put(sub_sub_string,num_new) ;
                    j++;
                }
                if(j ==words_num ){
                    result.add(i);
                }

            }
            return result;
        }

    public static void main(String[] args) {
        String s = "wordgoodgoodgoodbestword";
        String[] words = {"word","good","best","word"};
        List<Integer>  res = findSubstring(s,words);
        System.out.print(res);
        s.length();
    }


}
